package com.hyh.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hyh.ad.common.core.redis.CacheClient;
import com.hyh.ad.common.utils.SecurityUtils;
import com.hyh.admin.domain.Messages;
import com.hyh.admin.mapper.MessagesMapper;
import com.hyh.admin.service.IMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class MessageServiceImpl implements IMessageService {

    @Autowired
    private MessagesMapper messagesMapper;

    @Autowired
    private CacheClient cacheClient;

    @Override
    public List<Messages> listMessage(Long id) {
        //查询缓存当中是否有消息
        Long senderId = SecurityUtils.getLoginUser().getUser().getId();
        Long receiverId = id;
        //List<Messages> list = cacheClient.getList(ImConstant.MESSAGE_LIST + senderId + "&" + receiverId, Messages.class);
        //if(list != null && !list.isEmpty()){
        //    return list;
        //}
        LambdaQueryWrapper<Messages> query = new LambdaQueryWrapper<>();
        query.eq(Messages::getSenderId, senderId);
        query.eq(Messages::getReceiverId, receiverId).or()
                .eq(Messages::getSenderId, receiverId)
                .eq(Messages::getReceiverId, senderId);
        query.orderByAsc(Messages::getCreateTime); // 按时间降序排列 时间越晚越靠前
        List<Messages> messages = messagesMapper.selectList(query);
        //存入缓存
        //cacheClient.setListWithoutTime(ImConstant.MESSAGE_LIST + senderId + "&" + receiverId, messages);
        return  messages;
    }

    @Override
    public void updateMessageStatus(Long[] ids) {
        Arrays.stream(ids).forEach(id -> {
            Messages messages = messagesMapper.selectById(id);
            messages.setReadStatus("1");
            messagesMapper.updateById(messages);
        });
    }

    @Override
    public int getUnreadMessage(Long id) {
        Long senderId = SecurityUtils.getLoginUser().getUser().getId();
        Long receiverId = id;
        LambdaQueryWrapper<Messages> query = new LambdaQueryWrapper<>();
        query.eq(Messages::getSenderId, receiverId);
        query.eq(Messages::getReceiverId, senderId);
        query.eq(Messages::getReadStatus, "0");
        return messagesMapper.selectCount(query);
    }

    @Override
    public void addMessage(Messages messages) {
        messagesMapper.insert(messages);
    }

    @Override
    public int readMessage(Long id) {
        Long senderId = SecurityUtils.getLoginUser().getUser().getId();
        Long receiverId = id;
        LambdaQueryWrapper<Messages> query = new LambdaQueryWrapper<>();
        query.eq(Messages::getSenderId, receiverId);
        query.eq(Messages::getReceiverId, senderId);
        query.eq(Messages::getReadStatus, "0");
        List<Messages> messages = messagesMapper.selectList(query);
        Long[] ids = messages.stream().map(Messages::getId).toArray(Long[]::new);
        if (ids.length > 0) {
            updateMessageStatus(ids);
        }
        return messages.size();
    }

    @Override
    public Messages getLatestMessage(Long id) {
        Long senderId = SecurityUtils.getLoginUser().getUser().getId();
        Long receiverId = id;
        LambdaQueryWrapper<Messages> query = new LambdaQueryWrapper<>();
        query.eq(Messages::getSenderId, receiverId);
        query.eq(Messages::getReceiverId, senderId);
        query.orderByDesc(Messages::getCreateTime); // 按时间降序排列 时间越晚越靠前
        query.last("limit 1");
        return messagesMapper.selectOne(query);
    }
}
